package com.jt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RoleAndMenuMapper;
import com.jt.pojo.Result;
import com.jt.pojo.Role_Menu;
import com.jt.pojo.Menu;
import com.jt.service.RoleService;
import com.jt.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;
     @Autowired
     private RoleAndMenuMapper roleAndRouterMapper;
    /**
     * 查询菜单列表
     *
     * @return Result
     */
    @GetMapping("/selectMenuList")
    public Result selectRouterList() {
        List<Menu> routerList = menuService.selectRouterList();
        return new Result().setCode(200).setData(routerList).setMassage("查询菜单列表");
    }

    /**
     * 分配权限
     *
     * @return Result
     */
    @GetMapping("/distributionRouter")
    public Result distributionRouter(String menuList,String RowRoleId ) {
        /*分配之前先删除当前权限的路由表信息*/
          roleAndRouterMapper.delete(new QueryWrapper<Role_Menu>().eq("roleId", RowRoleId));
         String[] menuString = menuList.split(",");
         List<String> MenuIdList = Arrays.asList(menuString);
         int count=menuService.distributionRouter(MenuIdList,RowRoleId);
         if (count>0){
             log.info("新增{}条菜单数据成功",count);
             return  new Result().setCode(200).setData(count).setMassage("分配成功");
         }else {
             return  new Result().setCode(500).setData(count).setMassage("分配失败");
         }
    }

}
